package com.bsj.travel.util.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/***
 * @description ModelExcelListener
 * @author LiJiaXin
 * @version 1.0.0
 * @time 2024/1/16 11:56
 **/
@Data
@Slf4j
public class ModelExcelListener<T> extends AnalysisEventListener {

    private Map<Integer, T> data = new HashMap<Integer, T>();

    /**
     * When analysis one row trigger invoke function.
     *
     * @param object  one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(Object object, AnalysisContext context) {
        final T t = (T) object;
        Integer rowIndex = context.readRowHolder().getRowIndex() + 1;
        //数据存储到map，供批量处理，或后续自己业务逻辑处理。
        data.put(rowIndex, t);
    }

    /**
     * if have something to do after all analysis
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("excel parse success !!!");
    }
}
